// imageCompressor.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include "MyImage.h"
#include <conio.h>
#include <string>
#include <vector>
#include "NeuralNet.h"
#include "FileException.h"

using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{

	vector <vector <float>> samples;
	
	string name = "D:\\sandwitchGS.bmp";
	unsigned int x,y;
	unsigned int pixelsX = 7, pixelsY = 7, resolution=8; //resolution=8x8
	unsigned int pixelsX0 = 0, pixelsY0 = 0;
	unsigned int samplesX;
	unsigned int samplesY;
	unsigned int samplesAll;

	int counter = 1;
	try{
		MyImage<unsigned char> image(name);
		image.getImageResolution(&x,&y);
		samplesX = x/resolution;
		samplesY = y/resolution;
		samplesAll = samplesX*samplesY;
		samples.resize(samplesAll);
		for (unsigned int i=0;i<samplesAll;i++){
			MyImage<unsigned char> img(image.getImageBitMapSample(pixelsX0,pixelsY0,pixelsX,pixelsY));
			samples[i] = img.extractImageData();
			if (counter==samplesX){
				pixelsX0 =0;
				pixelsX = resolution-1;
				pixelsY0 += resolution;
				pixelsY += resolution;
				counter = 1;
			}else{
				pixelsX0 += resolution;
				pixelsX+=resolution;
				counter++;
			}
		} 
	}catch(IllegalArgumentException e){ 
		printf("%s",e.what()); 
		_getch();
		exit(-1);
	}catch(FileException e){
		printf("%s",e.what()); 
		_getch();
		exit(-2);
	}

	samples.resize(20);
	for(int i=0;i<20;i++){
		samples[i].resize(2);
	}

	vector<float> avg(samples[0].size());
	float sum=0;
	for (unsigned int j=0;j<samples[0].size();j++){
		for (unsigned int i=0; i< samples.size();i++){
			sum+=samples[i][j];
		}

		avg[j]=sum/samples.size();
		sum=0;
	}

	for (unsigned int j=0;j<samples[0].size();j++){
		for (unsigned int i=0; i< samples.size();i++){
			samples[i][j]=(samples[i][j]-avg[j]);
		}
	}


	unsigned int components = samples[0].size();
	unsigned int inputs = samples.size();
	unsigned int neurons = 2;
	vector<vector <float>> weights;
	vector<float> outpus(neurons);
	try{
		NeuralNetwork net(neurons,inputs,components);
		weights = net.calculateWithTrenerGHA(samples);
		outpus = net.getNeuronOutputs();
	}catch (IllegalArgumentException e){
		printf("%s",e.what()); 
	}

	int j;

	for (j=0;j<weights.size();j++){
		printf("w%d : \n",j);
		for (unsigned int i=0; i<weights[0].size();i++){
			printf("%f ",weights[j][i]);
		}
		printf("\n\n");
	}

	printf("outputs: \n");
	for (unsigned int j=0; j<neurons;j++){
		printf("%f,  ",outpus[j]);
	}
	
	_getch();
	return 0;
}

